package com.chen.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.chen.reggie.common.BaseContext;
import com.chen.reggie.common.R;
import com.chen.reggie.pojo.ShoppingCart;
import com.chen.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @ClassName ShoppingCartController
 * @Description TODO
 * @Author hainc
 * @Date 2022/9/6 21:22
 * @Version 1.0
 * 购物车
 **/
@Slf4j
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;


    /**
     * @date 2022/9/6
     * @param [shoppingCart]
     * @return com.chen.reggie.common.R<com.chen.reggie.pojo.ShoppingCart>
     *     添加到购物车
     **/
    @PostMapping("/add")
    public R<ShoppingCart> addShoppingCart(@RequestBody ShoppingCart shoppingCart){
        log.info("购物车数据：{}",shoppingCart);
        //把菜品或套餐添加到购物车

        //设置购物车的userId,因为前端没有传递用户id
        Long currentId = BaseContext.getCurrentId();
        shoppingCart.setUserId(currentId);

        //判断添加的菜品或套餐是否存在于购物车，如果是则number+1，否则设置number=1
        LambdaQueryWrapper<ShoppingCart> shoppingCartLambdaQueryWrapper = new LambdaQueryWrapper<>();
        shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getUserId,currentId);

        Long dishId = shoppingCart.getDishId();
        //判断传递的是菜品还是套餐
        if(dishId!=null){
            //菜品id不为空，则代表是菜品
            shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getDishId,dishId);
        }else{
            shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }

        ShoppingCart shopCart = shoppingCartService.getOne(shoppingCartLambdaQueryWrapper);
        if(shopCart!=null){
            //代表当前菜品或套餐存在于购物车，number+1
            shopCart.setNumber(shopCart.getNumber()+1);
            shoppingCartService.updateById(shopCart);
        }else{
            //添加则购物车
            shopCart = shoppingCart;
            shopCart.setNumber(1);
            shopCart.setCreateTime(LocalDateTime.now());
            shoppingCartService.save(shopCart);
        }

        return R.success(shopCart);

    }

    //查看购物车
    @GetMapping("/list")
    public R<List<ShoppingCart>> shoppingCartList(){
        log.info("查看购物车");
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
        queryWrapper.orderByAsc(ShoppingCart::getCreateTime);
        List<ShoppingCart> list = shoppingCartService.list(queryWrapper);

        return R.success(list);
    }

    //购物车减少商品数量
    @PostMapping("/sub")
    public R<ShoppingCart> subShop(@RequestBody ShoppingCart shoppingCart){
        log.info(shoppingCart.toString());
        //根据用户id查询对应购物车
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());

        //判断是减少菜品/套餐数量
        Long dishId = shoppingCart.getDishId();
        if(dishId!=null){
            //减少菜品
            queryWrapper.eq(ShoppingCart::getDishId,dishId);

        }else{
            //减少套餐
            queryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }
        //根据条件获取套餐/菜品对象，如果该商品number=0，则删除该条记录，否则number-1
        ShoppingCart shopCart = shoppingCartService.getOne(queryWrapper);
        if(shopCart==null){
            return R.error("当前列表为空，请添加套餐或菜品");
        }
        Integer number = shopCart.getNumber();
        if(number > 1){
            shopCart.setNumber(number-1);
            shoppingCartService.update(shopCart,queryWrapper);
        }else{
            shoppingCartService.remove(queryWrapper);
        }

        return R.success(shopCart);


    }


    //清空购物车
    @DeleteMapping("/clean")
    public R<String> cleanShoppingCart(){
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
        boolean remove = shoppingCartService.remove(queryWrapper);
        if(remove){
            return R.success("清空购物车成功");
        }
        return R.error("清空购物车失败");

    }


}
